import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:toatool/src/theme/app_theme.dart';
import 'view_state.dart';

/// 加载中
class ViewStateBusyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: CircularProgressIndicator());
  }
}

/// 基础Widget
class ViewStateWidget extends StatelessWidget {
  final String imageUrl;
  final String message;
  final String? buttonText;
  final VoidCallback? onPressed;

  ViewStateWidget({
    Key? key,
    required this.imageUrl,
    required this.message,
    this.buttonText,
    this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            imageUrl,
            height: ScreenUtil().setHeight(144),
          ),
          Padding(
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(30.0),
              right: ScreenUtil().setWidth(30.0),
              top: ScreenUtil().setWidth(24.0),
            ),
            child: ConstrainedBox(
              constraints: BoxConstraints(
                maxHeight: ScreenUtil().setWidth(200.0),
              ),
              child: SingleChildScrollView(
                child: Text(
                  message,
                  textAlign: TextAlign.center,
                  style: Theme.of(context)
                      .textTheme
                      .subtitle2!
                      .copyWith(color: moreLightGreyTextColor),
                ),
              ),
            ),
          ),
          if (buttonText != null && onPressed != null)
            TextButton(
              onPressed: onPressed,
              child: Text(
                buttonText ?? '',
                style: Theme.of(context)
                    .textTheme
                    .button!
                    .copyWith(color: Theme.of(context).primaryColor),
              ),
            )
        ],
      ),
    );
  }
}

/// ErrorWidget
class ViewStateErrorWidget extends StatelessWidget {
  final ViewStateError error;
  final String? buttonText;
  final VoidCallback? onPressed;

  const ViewStateErrorWidget({
    Key? key,
    required this.error,
    this.buttonText,
    this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    String defaultImageUrl;
    var defaultMessage = error.message;
    switch (error.errorType) {
      case ViewStateErrorType.cancelException:
        defaultImageUrl = 'assets/icons/dataError.png';
        defaultMessage = 'exception_of_request_cancel'.tr;
        break;
      case ViewStateErrorType.fetchDataException:
        defaultImageUrl = 'assets/icons/networkError.png';
        defaultMessage = 'exception_of_data'.tr;
        break;
      case ViewStateErrorType.httpException:
        defaultImageUrl = 'assets/icons/dataError.png';
        defaultMessage = 'exception_of_http'.tr;
        break;
      case ViewStateErrorType.networkException:
        defaultImageUrl = 'assets/icons/networkError.png';
        defaultMessage = 'exception_of_network'.tr;
        break;
      case ViewStateErrorType.unknowException:
        defaultImageUrl = 'assets/icons/dataError.png';
        defaultMessage = 'exception_of_unknown'.tr;
        break;
      case ViewStateErrorType.unAuthorizedException:
        defaultImageUrl = 'assets/icons/dataError.png';
        defaultMessage = 'exception_of_unauthorized'.tr;
        break;
      default:
        defaultImageUrl = 'assets/icons/dataError.png';
        defaultMessage = 'exception_of_data'.tr;
    }

    return ViewStateWidget(
      imageUrl: defaultImageUrl,
      message: defaultMessage,
      buttonText: buttonText,
      onPressed: onPressed,
    );
  }
}

/// 页面无数据
class ViewStateEmptyWidget extends StatelessWidget {
  final String? imageUrl;
  final String? message;
  final String? buttonText;
  final VoidCallback? onPressed;

  const ViewStateEmptyWidget({
    Key? key,
    this.imageUrl,
    this.message,
    this.buttonText,
    this.onPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewStateWidget(
      imageUrl: imageUrl ?? 'assets/icons/empty.png',
      message: message ?? 'no_data'.tr,
      buttonText: buttonText,
      onPressed: onPressed,
    );
  }
}

/// 页面未授权
class ViewStateUnAuthWidget extends StatelessWidget {
  final String imageUrl;
  final String message;
  final String buttonText;
  final VoidCallback onPressed;

  const ViewStateUnAuthWidget(
      {Key? key,
      required this.imageUrl,
      required this.message,
      required this.buttonText,
      required this.onPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ViewStateWidget(
      imageUrl: imageUrl,
      message: message,
      buttonText: buttonText,
      onPressed: onPressed,
    );
  }
}
